package cn.hy.preparelesson.common.utils;

import cn.hy.pms.thrift.SysUserThrift;
import cn.hy.pms.thrift.utils.ThriftUtils;
import org.springframework.util.CollectionUtils;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 用户信息Utils
 */
public class UserInfoUtils {
    /**
     * 封装用户名
     */
    @SuppressWarnings("all")
    public static void buildUserName(List targetList, String getUserIdMethodName, String setUserNameMethodName) {
        if (CollectionUtils.isEmpty(targetList)) {
            return;
        }

        Class targetClass = targetList.get(0).getClass();
        try {
            Method getUserIdMethod = targetClass.getMethod(getUserIdMethodName);
            Method setUserNameMethod = targetClass.getMethod(setUserNameMethodName, String.class);
            Set<String> userIdSet = new HashSet<>(targetList.size());
            for (Object objTmp  : targetList) {
                userIdSet.add((String) getUserIdMethod.invoke(objTmp));
            }

            List<SysUserThrift> userList = ThriftUtils.findUserByUserIdList(new ArrayList<>(userIdSet));
            if (CollectionUtils.isEmpty(userList)) {
                return;
            }
            for (Object tTmp : targetList) {
                String userId = (String) getUserIdMethod.invoke(tTmp);
                for (SysUserThrift suTmp : userList) {
                    if (suTmp.getId().equals(userId)) {
                        setUserNameMethod.invoke(tTmp, suTmp.getRealName());
                        break;
                    }
                }
            }

        } catch (Exception e) {}
    }

}
